package com.kw.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kw.drug.common.ResultMapUtil;
import com.kw.drug.entity.Druginfo;
import com.kw.drug.entity.Saleinfo;
import com.kw.drug.service.SaleinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/saleinfo")
public class SaleinfoController {
    @Autowired
    SaleinfoService saleinfoService;

    @RequestMapping()
    public String saleinfoIndex() {
        return "saleinfo";
    }

    /*
    分页查询记录
    * */

    /**
     * 分页查询药品出入库列表
     */
    @RequestMapping(value = "/saleinfoQueryPage")
    @ResponseBody
    public Map owinfoQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<Saleinfo> iPage = saleinfoService.selectDruginfoPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 编辑页面
     */


    @RequestMapping("/saleinfoQueryById")
    public String editIndex(@RequestParam(name = "id", required = true) Integer id, Model model) {
        Saleinfo saleinfo = saleinfoService.selectById(id);
        model.addAttribute("obj", saleinfo);
        return "saleinfoPage";
    }

    /*
    修改记录
    * */
    @RequestMapping("/saleinfoEdit")
    @ResponseBody
    public Map saleinfoEdit(Saleinfo saleinfo) {
        try {
            Integer integer = saleinfoService.updateSaleInfo(saleinfo);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除记录
     */
    @RequestMapping("/saleinfoDelById")
    @ResponseBody
    public Map saleinfoDelById(Integer id) {
        try {
            Integer integer = saleinfoService.deleteSaleInfoById(id);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/saleinfoPage")
    public String addIndex() {
        return "saleinfoPage";
    }

    /**
     * 添加记录
     */
    @RequestMapping("/saleinfoAdd")
    @ResponseBody
    public Map saleinfoAdd(Saleinfo saleinfo) {
        try {
            Integer integer = saleinfoService.addSaleInfo(saleinfo);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
}
